{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "# Submit Training Job Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import time\n",
    "import os\n",
    "import requests\n",
    "\n",
    "import fml_manager\n",
    "from fml_manager import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## For more details about the FMLManager, please refer to this [document](https://kubefate.readthedocs.io/README.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "The DSL and Config file can be presented in JOSN format. \n",
    "Submitting a job with JSON defined in line, can use ```submit_job(self,dsl, config)```. \n",
    "Note: the parameters are dict, the JSON string have to transform to dict with ```json.loads```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# dsl\n",
    "secure_add_example = Component(name='secure_add_example_0',\n",
    "                               module='SecureAddExample')\n",
    "\n",
    "pipeline = Pipeline(\n",
    "    secure_add_example\n",
    ")\n",
    "\n",
    "# Configuration\n",
    "initiator = Initiator(role='guest',\n",
    "                      party_id=9999)\n",
    "\n",
    "job_parameters = JobParameters(work_mode=1)\n",
    "\n",
    "role = RoleBuilder()\\\n",
    "    .add_guest(9999)\\\n",
    "    .add_host(9999).build()\n",
    "\n",
    "secure_add_example_guest_config = {\n",
    "    \"seed\": [\n",
    "        123\n",
    "    ]\n",
    "}\n",
    "\n",
    "secure_add_example_host_config = {\n",
    "    \"seed\": [\n",
    "        321\n",
    "    ]\n",
    "}\n",
    "role_parameters = RoleParametersBuilder()\\\n",
    "    .add_host_module_config(module='secure_add_example_0', config=secure_add_example_host_config)\\\n",
    "    .add_guest_module_config(module='secure_add_example_0', config=secure_add_example_guest_config).build()\n",
    "\n",
    "secure_add_example = {\n",
    "    \"partition\": 10,\n",
    "    \"data_num\": 1000\n",
    "}\n",
    "\n",
    "algorithm_parameters = AlgorithmParametersBuilder()\\\n",
    "    .add_module_config(module='secure_add_example_0', config=secure_add_example).build()\n",
    "\n",
    "config = Config(\n",
    "    initiator,\n",
    "    job_parameters,\n",
    "    role,\n",
    "    role_parameters,\n",
    "    algorithm_parameters\n",
    ")\n",
    "\n",
    "manager = fml_manager.FMLManager()\n",
    "response = manager.submit_job(pipeline.to_dict(), config.to_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success!\n",
      "{\n",
      "    \"data\": {\n",
      "        \"board_url\": \"http://fateboard:8080/index.html#/dashboard?job_id=202109140844000248365&role=guest&party_id=9999\",\n",
      "        \"job_dsl_path\": \"/data/projects/fate/jobs/202109140844000248365/job_dsl.json\",\n",
      "        \"job_id\": \"202109140844000248365\",\n",
      "        \"job_runtime_conf_on_party_path\": \"/data/projects/fate/jobs/202109140844000248365/guest/job_runtime_on_party_conf.json\",\n",
      "        \"job_runtime_conf_path\": \"/data/projects/fate/jobs/202109140844000248365/job_runtime_conf.json\",\n",
      "        \"logs_directory\": \"/data/projects/fate/logs/202109140844000248365\",\n",
      "        \"model_info\": {\n",
      "            \"model_id\": \"arbiter-#guest-9999#host-9999#model\",\n",
      "            \"model_version\": \"202109140844000248365\"\n",
      "        },\n",
      "        \"pipeline_dsl_path\": \"/data/projects/fate/jobs/202109140844000248365/pipeline_dsl.json\",\n",
      "        \"train_runtime_conf_path\": \"/data/projects/fate/jobs/202109140844000248365/train_runtime_conf.json\"\n",
      "    },\n",
      "    \"jobId\": \"202109140844000248365\",\n",
      "    \"retcode\": 0,\n",
      "    \"retmsg\": \"success\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "manager.prettify(response, verbose=True)\n",
    "stdout = json.loads(response.content)\n",
    "job_id = stdout['jobId']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success!\n",
      "{\n",
      "    \"data\": [\n",
      "        {\n",
      "            \"f_apply_resource_time\": 1631609042226,\n",
      "            \"f_cancel_signal\": false,\n",
      "            \"f_cancel_time\": null,\n",
      "            \"f_cores\": 4,\n",
      "            \"f_create_date\": \"Tue, 14 Sep 2021 08:44:00 GMT\",\n",
      "            \"f_create_time\": 1631609040712,\n",
      "            \"f_description\": \"\",\n",
      "            \"f_dsl\": {\n",
      "                \"components\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"module\": \"SecureAddExample\",\n",
      "                        \"need_deploy\": true\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_elapsed\": 13282,\n",
      "            \"f_end_date\": \"Tue, 14 Sep 2021 08:44:15 GMT\",\n",
      "            \"f_end_scheduling_updates\": 1,\n",
      "            \"f_end_time\": 1631609055705,\n",
      "            \"f_engine_name\": \"EGGROLL\",\n",
      "            \"f_engine_type\": \"computing\",\n",
      "            \"f_initiator_party_id\": \"9999\",\n",
      "            \"f_initiator_role\": \"guest\",\n",
      "            \"f_is_initiator\": true,\n",
      "            \"f_job_id\": \"202109140844000248365\",\n",
      "            \"f_memory\": 0,\n",
      "            \"f_name\": \"\",\n",
      "            \"f_party_id\": \"9999\",\n",
      "            \"f_progress\": 100,\n",
      "            \"f_ready_signal\": false,\n",
      "            \"f_ready_time\": null,\n",
      "            \"f_remaining_cores\": 4,\n",
      "            \"f_remaining_memory\": 0,\n",
      "            \"f_rerun_signal\": false,\n",
      "            \"f_resource_in_use\": false,\n",
      "            \"f_return_resource_time\": 1631609055709,\n",
      "            \"f_role\": \"guest\",\n",
      "            \"f_roles\": {\n",
      "                \"arbiter\": [],\n",
      "                \"guest\": [\n",
      "                    9999\n",
      "                ],\n",
      "                \"host\": [\n",
      "                    9999\n",
      "                ]\n",
      "            },\n",
      "            \"f_runtime_conf\": {\n",
      "                \"algorithm_parameters\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"data_num\": 1000,\n",
      "                        \"partition\": 10\n",
      "                    }\n",
      "                },\n",
      "                \"initiator\": {\n",
      "                    \"party_id\": 9999,\n",
      "                    \"role\": \"guest\"\n",
      "                },\n",
      "                \"job_parameters\": {\n",
      "                    \"adaptation_parameters\": {\n",
      "                        \"if_initiator_baseline\": true,\n",
      "                        \"request_task_cores\": 4,\n",
      "                        \"task_cores_per_node\": 4,\n",
      "                        \"task_memory_per_node\": 0,\n",
      "                        \"task_nodes\": 1\n",
      "                    },\n",
      "                    \"backend\": 0,\n",
      "                    \"computing_engine\": \"EGGROLL\",\n",
      "                    \"computing_partitions\": 4,\n",
      "                    \"eggroll_run\": {},\n",
      "                    \"engines_address\": {},\n",
      "                    \"federated_mode\": \"MULTIPLE\",\n",
      "                    \"federated_status_collect_type\": \"PULL\",\n",
      "                    \"federation_engine\": \"EGGROLL\",\n",
      "                    \"job_type\": \"train\",\n",
      "                    \"model_id\": \"arbiter-#guest-9999#host-9999#model\",\n",
      "                    \"model_version\": \"202109140844000248365\",\n",
      "                    \"pulsar_run\": {},\n",
      "                    \"rabbitmq_run\": {},\n",
      "                    \"spark_run\": {},\n",
      "                    \"storage_engine\": \"EGGROLL\",\n",
      "                    \"task_parallelism\": 1,\n",
      "                    \"work_mode\": 1\n",
      "                },\n",
      "                \"role\": {\n",
      "                    \"arbiter\": [],\n",
      "                    \"guest\": [\n",
      "                        9999\n",
      "                    ],\n",
      "                    \"host\": [\n",
      "                        9999\n",
      "                    ]\n",
      "                },\n",
      "                \"role_parameters\": {\n",
      "                    \"guest\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                123\n",
      "                            ]\n",
      "                        }\n",
      "                    },\n",
      "                    \"host\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                321\n",
      "                            ]\n",
      "                        }\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_runtime_conf_on_party\": {\n",
      "                \"algorithm_parameters\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"data_num\": 1000,\n",
      "                        \"partition\": 10\n",
      "                    }\n",
      "                },\n",
      "                \"initiator\": {\n",
      "                    \"party_id\": 9999,\n",
      "                    \"role\": \"guest\"\n",
      "                },\n",
      "                \"job_parameters\": {\n",
      "                    \"adaptation_parameters\": {\n",
      "                        \"if_initiator_baseline\": false,\n",
      "                        \"request_task_cores\": 4,\n",
      "                        \"task_cores_per_node\": 4,\n",
      "                        \"task_memory_per_node\": 0,\n",
      "                        \"task_nodes\": 1\n",
      "                    },\n",
      "                    \"backend\": 0,\n",
      "                    \"computing_engine\": \"EGGROLL\",\n",
      "                    \"computing_partitions\": 4,\n",
      "                    \"eggroll_run\": {\n",
      "                        \"eggroll.session.processors.per.node\": 4\n",
      "                    },\n",
      "                    \"engines_address\": {\n",
      "                        \"computing\": {\n",
      "                            \"cores_per_node\": 20,\n",
      "                            \"nodes\": 1\n",
      "                        },\n",
      "                        \"federation\": {\n",
      "                            \"host\": \"rollsite\",\n",
      "                            \"port\": 9370\n",
      "                        },\n",
      "                        \"storage\": {\n",
      "                            \"cores_per_node\": 20,\n",
      "                            \"nodes\": 1\n",
      "                        }\n",
      "                    },\n",
      "                    \"federated_mode\": \"MULTIPLE\",\n",
      "                    \"federated_status_collect_type\": \"PULL\",\n",
      "                    \"federation_engine\": \"EGGROLL\",\n",
      "                    \"job_type\": \"train\",\n",
      "                    \"model_id\": \"arbiter-#guest-9999#host-9999#model\",\n",
      "                    \"model_version\": \"202109140844000248365\",\n",
      "                    \"pulsar_run\": {},\n",
      "                    \"rabbitmq_run\": {},\n",
      "                    \"spark_run\": {},\n",
      "                    \"storage_engine\": \"EGGROLL\",\n",
      "                    \"task_parallelism\": 1,\n",
      "                    \"work_mode\": 1\n",
      "                },\n",
      "                \"role\": {\n",
      "                    \"arbiter\": [],\n",
      "                    \"guest\": [\n",
      "                        9999\n",
      "                    ],\n",
      "                    \"host\": [\n",
      "                        9999\n",
      "                    ]\n",
      "                },\n",
      "                \"role_parameters\": {\n",
      "                    \"guest\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                123\n",
      "                            ]\n",
      "                        }\n",
      "                    },\n",
      "                    \"host\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                321\n",
      "                            ]\n",
      "                        }\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_start_date\": \"Tue, 14 Sep 2021 08:44:02 GMT\",\n",
      "            \"f_start_time\": 1631609042423,\n",
      "            \"f_status\": \"success\",\n",
      "            \"f_status_code\": null,\n",
      "            \"f_tag\": \"job_end\",\n",
      "            \"f_train_runtime_conf\": {},\n",
      "            \"f_update_date\": \"Tue, 14 Sep 2021 08:44:00 GMT\",\n",
      "            \"f_update_time\": 1631609040713,\n",
      "            \"f_user_id\": null,\n",
      "            \"f_work_mode\": 1\n",
      "        },\n",
      "        {\n",
      "            \"f_apply_resource_time\": 1631609042329,\n",
      "            \"f_cancel_signal\": false,\n",
      "            \"f_cancel_time\": null,\n",
      "            \"f_cores\": 4,\n",
      "            \"f_create_date\": \"Tue, 14 Sep 2021 08:44:00 GMT\",\n",
      "            \"f_create_time\": 1631609040598,\n",
      "            \"f_description\": \"\",\n",
      "            \"f_dsl\": {\n",
      "                \"components\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"module\": \"SecureAddExample\",\n",
      "                        \"need_deploy\": true\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_elapsed\": 13256,\n",
      "            \"f_end_date\": \"Tue, 14 Sep 2021 08:44:15 GMT\",\n",
      "            \"f_end_scheduling_updates\": 1,\n",
      "            \"f_end_time\": 1631609055787,\n",
      "            \"f_engine_name\": \"EGGROLL\",\n",
      "            \"f_engine_type\": \"computing\",\n",
      "            \"f_initiator_party_id\": \"9999\",\n",
      "            \"f_initiator_role\": \"guest\",\n",
      "            \"f_is_initiator\": false,\n",
      "            \"f_job_id\": \"202109140844000248365\",\n",
      "            \"f_memory\": 0,\n",
      "            \"f_name\": \"\",\n",
      "            \"f_party_id\": \"9999\",\n",
      "            \"f_progress\": 100,\n",
      "            \"f_ready_signal\": false,\n",
      "            \"f_ready_time\": null,\n",
      "            \"f_remaining_cores\": 8,\n",
      "            \"f_remaining_memory\": 0,\n",
      "            \"f_rerun_signal\": false,\n",
      "            \"f_resource_in_use\": false,\n",
      "            \"f_return_resource_time\": 1631609055792,\n",
      "            \"f_role\": \"host\",\n",
      "            \"f_roles\": {\n",
      "                \"arbiter\": [],\n",
      "                \"guest\": [\n",
      "                    9999\n",
      "                ],\n",
      "                \"host\": [\n",
      "                    9999\n",
      "                ]\n",
      "            },\n",
      "            \"f_runtime_conf\": {\n",
      "                \"algorithm_parameters\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"data_num\": 1000,\n",
      "                        \"partition\": 10\n",
      "                    }\n",
      "                },\n",
      "                \"initiator\": {\n",
      "                    \"party_id\": 9999,\n",
      "                    \"role\": \"guest\"\n",
      "                },\n",
      "                \"job_parameters\": {\n",
      "                    \"adaptation_parameters\": {\n",
      "                        \"if_initiator_baseline\": true,\n",
      "                        \"request_task_cores\": 4,\n",
      "                        \"task_cores_per_node\": 4,\n",
      "                        \"task_memory_per_node\": 0,\n",
      "                        \"task_nodes\": 1\n",
      "                    },\n",
      "                    \"backend\": 0,\n",
      "                    \"computing_engine\": \"EGGROLL\",\n",
      "                    \"computing_partitions\": 4,\n",
      "                    \"eggroll_run\": {},\n",
      "                    \"engines_address\": {},\n",
      "                    \"federated_mode\": \"MULTIPLE\",\n",
      "                    \"federated_status_collect_type\": \"PULL\",\n",
      "                    \"federation_engine\": \"EGGROLL\",\n",
      "                    \"job_type\": \"train\",\n",
      "                    \"model_id\": \"arbiter-#guest-9999#host-9999#model\",\n",
      "                    \"model_version\": \"202109140844000248365\",\n",
      "                    \"pulsar_run\": {},\n",
      "                    \"rabbitmq_run\": {},\n",
      "                    \"spark_run\": {},\n",
      "                    \"storage_engine\": \"EGGROLL\",\n",
      "                    \"task_parallelism\": 1,\n",
      "                    \"work_mode\": 1\n",
      "                },\n",
      "                \"role\": {\n",
      "                    \"arbiter\": [],\n",
      "                    \"guest\": [\n",
      "                        9999\n",
      "                    ],\n",
      "                    \"host\": [\n",
      "                        9999\n",
      "                    ]\n",
      "                },\n",
      "                \"role_parameters\": {\n",
      "                    \"guest\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                123\n",
      "                            ]\n",
      "                        }\n",
      "                    },\n",
      "                    \"host\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                321\n",
      "                            ]\n",
      "                        }\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_runtime_conf_on_party\": {\n",
      "                \"algorithm_parameters\": {\n",
      "                    \"secure_add_example_0\": {\n",
      "                        \"data_num\": 1000,\n",
      "                        \"partition\": 10\n",
      "                    }\n",
      "                },\n",
      "                \"initiator\": {\n",
      "                    \"party_id\": 9999,\n",
      "                    \"role\": \"guest\"\n",
      "                },\n",
      "                \"job_parameters\": {\n",
      "                    \"adaptation_parameters\": {\n",
      "                        \"if_initiator_baseline\": false,\n",
      "                        \"request_task_cores\": 4,\n",
      "                        \"task_cores_per_node\": 4,\n",
      "                        \"task_memory_per_node\": 0,\n",
      "                        \"task_nodes\": 1\n",
      "                    },\n",
      "                    \"backend\": 0,\n",
      "                    \"computing_engine\": \"EGGROLL\",\n",
      "                    \"computing_partitions\": 4,\n",
      "                    \"eggroll_run\": {\n",
      "                        \"eggroll.session.processors.per.node\": 4\n",
      "                    },\n",
      "                    \"engines_address\": {\n",
      "                        \"computing\": {\n",
      "                            \"cores_per_node\": 20,\n",
      "                            \"nodes\": 1\n",
      "                        },\n",
      "                        \"federation\": {\n",
      "                            \"host\": \"rollsite\",\n",
      "                            \"port\": 9370\n",
      "                        },\n",
      "                        \"storage\": {\n",
      "                            \"cores_per_node\": 20,\n",
      "                            \"nodes\": 1\n",
      "                        }\n",
      "                    },\n",
      "                    \"federated_mode\": \"MULTIPLE\",\n",
      "                    \"federated_status_collect_type\": \"PULL\",\n",
      "                    \"federation_engine\": \"EGGROLL\",\n",
      "                    \"job_type\": \"train\",\n",
      "                    \"model_id\": \"arbiter-#guest-9999#host-9999#model\",\n",
      "                    \"model_version\": \"202109140844000248365\",\n",
      "                    \"pulsar_run\": {},\n",
      "                    \"rabbitmq_run\": {},\n",
      "                    \"spark_run\": {},\n",
      "                    \"storage_engine\": \"EGGROLL\",\n",
      "                    \"task_parallelism\": 1,\n",
      "                    \"work_mode\": 1\n",
      "                },\n",
      "                \"role\": {\n",
      "                    \"arbiter\": [],\n",
      "                    \"guest\": [\n",
      "                        9999\n",
      "                    ],\n",
      "                    \"host\": [\n",
      "                        9999\n",
      "                    ]\n",
      "                },\n",
      "                \"role_parameters\": {\n",
      "                    \"guest\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                123\n",
      "                            ]\n",
      "                        }\n",
      "                    },\n",
      "                    \"host\": {\n",
      "                        \"secure_add_example_0\": {\n",
      "                            \"seed\": [\n",
      "                                321\n",
      "                            ]\n",
      "                        }\n",
      "                    }\n",
      "                }\n",
      "            },\n",
      "            \"f_start_date\": \"Tue, 14 Sep 2021 08:44:02 GMT\",\n",
      "            \"f_start_time\": 1631609042531,\n",
      "            \"f_status\": \"success\",\n",
      "            \"f_status_code\": null,\n",
      "            \"f_tag\": \"job_end\",\n",
      "            \"f_train_runtime_conf\": {},\n",
      "            \"f_update_date\": \"Tue, 14 Sep 2021 08:44:00 GMT\",\n",
      "            \"f_update_time\": 1631609040598,\n",
      "            \"f_user_id\": null,\n",
      "            \"f_work_mode\": 1\n",
      "        }\n",
      "    ],\n",
      "    \"retcode\": 0,\n",
      "    \"retmsg\": \"success\"\n",
      "}\n",
      "Status: success\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'success'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_condition = QueryCondition(job_id)\n",
    "\n",
    "job_status = manager.query_job(query_condition)\n",
    "manager.prettify(job_status, verbose=True)\n",
    "manager.query_job_status(query_condition, max_tries=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "We can also fetch the logs of job submitted, and save it to working folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'retcode': 0, 'directory': './job_202109140844000248365_log', 'retmsg': 'download successfully, please check ./job_202109140844000248365_log directory, file name is job_202109140844000248365_log.tar.gz'}\n"
     ]
    }
   ],
   "source": [
    "response = manager.fetch_job_log(job_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
